﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using AIC_KQKT.Reports;
namespace AIC_KQKT
{
    public partial class MyReportView : MyFromBase
    {

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection(SqlHelper.connectionString);
                SqlCommand cmd = new SqlCommand(MyReportSQL, conn);

                for (int i = 0; i < MyReportParams.Count; i++)
                {
                    cmd.Parameters.AddWithValue(MyReportParams[i].ParameterName, MyReportParams[i].Value);
                }
                ReportDataSet ds = new ReportDataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                //da.Fill(ds.tblDanhSachChuTichCongDoanCoSoTheoNhiemKy);
                da.Fill(ds, MyTableName);

                ReportDocument objReport = new ReportDocument();
                objReport.Load(Server.MapPath(@"Reports/" + MyReportName));
                objReport.SetDatabaseLogon("sa", "tcct2013");
                objReport.SetDataSource(ds);

                #region Add param

                CrystalDecisions.Shared.ParameterDiscreteValue crParaDisValue = new CrystalDecisions.Shared.ParameterDiscreteValue();
                CrystalDecisions.Shared.ParameterValues crParaValue = new CrystalDecisions.Shared.ParameterValues();

                if (!MyReportSQLParam.Equals(""))
                {
                    List<SqlParameter> lstParams = new List<SqlParameter>();

                    DataTable dtParam = SqlHelper.ExecuteDataTable(CommandType.Text, MyReportSQLParam, MyReportParams);
                    if (dtParam.Rows.Count > 0)
                        for (int i = 0; i < dtParam.Columns.Count; i++)
                        {
                            crParaDisValue.Value = dtParam.Rows[0][i];
                            crParaValue.Clear();
                            crParaValue.Add(crParaDisValue);
                            objReport.DataDefinition.ParameterFields[dtParam.Columns[i].ColumnName.ToString()].ApplyCurrentValues(crParaValue);
                        }
                }
                #endregion Add param

                printReport.ReportSource = objReport;
                printReport.DataBind();


            }
            catch (Exception ex)
            {
                Response.Write("Exception Message: " + ex.StackTrace + "<br>");
            }
        }

    }
}
